///--------------------------------------------------
/**
 *
 *      Hand Gesture Recognition System
 *
 */
//--------------------------------------------------
/**
 * //CAMDISPLAY CLASS//
 * Clase para mostrar frames en la interfaz
 *
 * @author  Edwin Cobos & Cesar Murcia & Wilson Sarmiento
 * @version HGR_v1.5
 *
 * Revisado: 22/02/12
 */
//--------------------------------------------------
//PACKAGE//
package HGR;
//IMPORT//
import java.awt.color.ColorSpace;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.ComponentColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
//--------------------------------------------------
/**
 * Clase para mostrar frames en la interfaz, Heredada de la clase Thread de Java
 */
public class CamDisplay extends Thread
{
    //--------------------------------------------------
    private WritableRaster      rast;
    private DataBuffer          db;
    private ColorSpace          cs;
    private ColorModel          cm;
    private float               scale;
    private AffineTransform     at;
    private AffineTransformOp   ScaleOp;
    private byte[]              bufferByte;
    private Frame               buffer;
    private JLabel              out;
    private BufferedImage       img;
    private ImageIcon           images;
    //--------------------------------------------------
    /**
     * Constructor parametrico
     * @param bf Frame de la imagen a mostrar
     * @param _out Contenedor donde se mostrara la imagen
     */
    public CamDisplay(Frame bf, JLabel _out)
    {
        out = _out;
        buffer = bf;
        scale = (160.0f/buffer.getWidth());
        cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
        cm = new ComponentColorModel(cs,new int[]{8}, false, true,java.awt.Transparency.OPAQUE,DataBuffer.TYPE_BYTE);
        img = null;
    }    
    //--------------------------------------------------
    /**
     * Ejecuta el proceso
     */
    @Override
    public void run()
    {
        bufferByte = new byte[buffer.getBuffer().length];
        for(int k=0; k<bufferByte.length; k++)
        {
            bufferByte[k] = (byte)buffer.getBuffer()[k];
        }
        db = new DataBufferByte(bufferByte,bufferByte.length);
        rast = Raster.createInterleavedRaster(db, buffer.getWidth(), buffer.getHeight(), buffer.getWidth(), 1, new int[]{0}, null);
        img = new BufferedImage(cm, rast, true, null);
        at = new AffineTransform();
        at.scale(scale, scale);
        ScaleOp = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
        img = ScaleOp.filter(img, null);
        images = new ImageIcon(img);        
        out.setIcon(images);
    }
    //--------------------------------------------------
}
//--------------------------------------------------
//FIN
//--------------------------------------------------